package cn.smbms.servlet.provider;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;

import cn.smbms.pojo.Page;
import cn.smbms.pojo.Provider;
import cn.smbms.pojo.User;
import cn.smbms.service.provider.ProviderService;
import cn.smbms.service.provider.ProviderServiceImpl;
import cn.smbms.service.user.UserService;
import cn.smbms.service.user.UserServiceImpl;
import cn.smbms.tools.Constants;

public class ProviderServlet extends HttpServlet {
	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method = request.getParameter("method");
		if (method != null && method.equals("add")) {
			this.add(request, response);
		}
		else if (method != null && method.equals("query")) {
			this.query(request, response);
		}else if (method != null && method.equals("view")) {
			this.view(request, response,"jsp/providerview.jsp");
		}else if (method != null && method.equals("modify")) {
			this.view(request, response,"jsp/providermodify.jsp");
		}else if (method != null && method.equals("modifysave")) {
			this.modify(request, response);
		}else if (method != null && method.equals("delprovider")) {
			this.delprovider(request, response);
		}
		
	}



	private void delprovider(HttpServletRequest request,
			HttpServletResponse response)throws ServletException, IOException {
		String id = request.getParameter("proid");
		HashMap<String, String> resultMap = new HashMap<String, String>();
		if(!StringUtils.isNullOrEmpty(id)){
			ProviderService providerService = new ProviderServiceImpl();
			int flag = providerService.deleteProviderById(id);
			if(flag == 0){//删除成功
				resultMap.put("delResult", "true");
			}else if(flag == -1){
				resultMap.put("delResult", "false");
			}else if(flag > 0){
				resultMap.put("delResult", String.valueOf(flag));
			}
		}else{
			resultMap.put("delResult", "notexit");
		}
		response.setContentType("application/json");
		PrintWriter outPrintWriter = response.getWriter();
		outPrintWriter.write(JSONArray.toJSONString(resultMap));
		outPrintWriter.flush();
		outPrintWriter.close();
		
		
	}

	private void modify(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		String proContact = request.getParameter("proContact");
		String proPhone = request.getParameter("proPhone");
		String proAddress = request.getParameter("proAddress");
		String proFax = request.getParameter("proFax");
		String proDesc = request.getParameter("proDesc");
		String id = request.getParameter("id");
		Provider provider = new Provider();
		provider.setId(Integer.valueOf(id));
		provider.setProContact(proContact);
		provider.setProPhone(proPhone);
		provider.setProFax(proFax);
		provider.setProAddress(proAddress);
		provider.setProDesc(proDesc);
		provider.setModifyBy(((User)request.getSession().getAttribute(Constants.USER_SESSION)).getId());
		provider.setModifyDate(new Date());
		boolean flag = false;
		ProviderService providerService = new ProviderServiceImpl();
		flag = providerService.modify(provider);
		if(flag){
			request.getRequestDispatcher("/provider.do?method=query").forward(request, response);
		}else{
			request.getRequestDispatcher("jsp/providermodify.jsp").forward(request, response);
		}
	}

	private void view(HttpServletRequest request, HttpServletResponse response,String url)throws ServletException, IOException {
		String id = request.getParameter("proid");
		if(id!=null&&id!=""){
			Provider provider=null;
			ProviderService providerService= new ProviderServiceImpl();
			provider=providerService.getProviderById(id);
			request.setAttribute("provider", provider);
			request.getRequestDispatcher(url).forward(request, response);
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String proCode = request.getParameter("proCode");
		String proName = request.getParameter("proName");
		String proContact = request.getParameter("proContact");
		String proPhone = request.getParameter("proPhone");
		String proAddress = request.getParameter("proAddress");
		String proFax = request.getParameter("proFax");
		String proDesc = request.getParameter("proDesc");
		Provider provider = new Provider();
		provider.setProCode(proCode);
		provider.setProName(proName);
		provider.setProContact(proContact);
		provider.setProPhone(proPhone);
		provider.setProAddress(proAddress);
		provider.setProFax(proFax);
		provider.setProDesc(proDesc);
		provider.setCreatedBy(((User) request.getSession().getAttribute(
				Constants.USER_SESSION)).getId());
		provider.setCreationDate(new Date());
		ProviderService providerService = new ProviderServiceImpl();
		if (providerService.add(provider)) {
			request.getRequestDispatcher("/provider.do?method=query").forward(
					request, response);
		} else {
			request.getRequestDispatcher("jsp/provideradd.jsp").forward(
					request, response);
		}
	}

	private void query(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ProviderService providerService = new ProviderServiceImpl();
		String queryProName = request.getParameter("queryProName");
		if (queryProName == null) {
			queryProName = "";
		}
		String index = request.getParameter("pageIndex");
		int pageIndex;
		try {
			pageIndex = Integer.parseInt(index);
		} catch (NumberFormatException e) {
			pageIndex = 1;
		}
		Page page = new Page();
		int counts = providerService.getCounts(queryProName);
		// 总页数
		page.setCounts(counts);
		page.setPageIndex(pageIndex);
		int pageSize = 3;
		page.setPageSize(pageSize);
		List<Provider> list = providerService.getProviderList(queryProName, pageIndex, pageSize);
		request.setAttribute("page", page);
		request.setAttribute("providerList", list);
		request.setAttribute("queryProName", queryProName);
		request.getRequestDispatcher("jsp/providerlist.jsp").forward(request,
				response);
	}

}
